import json

from django import http
from django.shortcuts import render

# Create your views here.
from django.views import View
from django_redis import get_redis_connection

from apps.users.models import User
from utils.scert import SecretOauth

class ThirdView(View):
    def post(self, request, user_id):
        dict = json.loads(request.body.decode())

        password = dict.get('password')
        password2 = dict.get('password2')
        access_token = dict.get('access_token')

        if password != password2:
            return http.HttpResponseForbidden('两次密码输入不一致，请重新输入')
        img_client = get_redis_connection('verify_image_code')
        token = img_client.get('mobile').decode()
        print(token, type(token))
        access_token = SecretOauth().loads(access_token)
        print(access_token, type(access_token))
        if token != access_token:
            return  http.JsonResponse({
                    "status": 5001,
                    'message': "token错误!"
                })
        try:
          a = User.objects.get(id=user_id)
          print(a.id)
          a.set_password(password)
          a.save()
        except:
            return http.JsonResponse({"status": 5002})

        return http.JsonResponse({'status':5000, 'message':'修改成功'})


class Second1View(View):
    def get(self, request,mobile):
        sms_code = request.GET.get('sms_code')
        try:
            user = User.objects.get(mobile=mobile)
        except Exception as e:
            return  http.JsonResponse({"status":5004})
        access_token = SecretOauth().dumps(str(mobile))
        return http.JsonResponse({
        "status": 5000,
        "user_id": user.id,
        "access_token": access_token
})


class SecondView(View):
    def get(self, request, mobile):
        token = request.GET.get('access_token')
        access_token = SecretOauth.loads(token)
        img_client = get_redis_connection('verify_image_code')
        if img_client.get('mobile') != access_token:
            return http.JsonResponse({'access_token':5001})
        return http.JsonResponse({'status':5000, 'message':'短信发送成功'})
class FirstView(View):
    def get(self, request,username):
        # 1.接受参数
        image_code = request.GET.get('image_code')
        uuid = request.GET.get('image_code_id')
        try:
            user = User.objects.get(username=username)
        except User.DoesNotExist:
            return http.JsonResponse({'status': 5004})
        img_client = get_redis_connection('verify_image_code')
        redis_img_code = img_client.get('img_%s' % uuid)
        if image_code.lower() !=  redis_img_code.decode().lower():
            return http.JsonResponse({'status': 5001})
        mobile = user.mobile

        img_client.setex('mobile', 300, mobile)
        access_token = SecretOauth().dumps(str(mobile))
        return http.JsonResponse({'status': 5000, "mobile": mobile, "access_token": access_token})
# 展示页面
class  FindPasswordView(View):
    def get(self, request):
        return render(request, 'find_password.html')